package main

import (
	"fmt"
	"sync"
	"time"
)

// 计算各个数阶层放入到map
// 多个协程工作
var myMap = make(map[int]int, 10)

// 全局互斥锁 --低级
var lock sync.Mutex

func test(n int) {
	res := 1
	for i := 1; i <= n; i++ {
		res *= i
	}
	lock.Lock()
	myMap[n] = res
	lock.Unlock()
}
func main() {
	for i := 1; i <= 20; i++ {
		go test(i)
	}

	time.Sleep(5 * 1000)
	lock.Lock()
	for i, v := range myMap {
		fmt.Printf("map[%d]%v\n", i, v)
	}
	lock.Unlock()
}
